package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.databinding.HzklActivityResetPwdBinding;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserResetPwdActivity extends BaseActivity {
    private HzklActivityResetPwdBinding binding;
    @Inject
    UserRepo repo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    boolean oldEnable, newEnable;
    ////密码只包含字母，数字，字符中至少两种
    private static final String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,32}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_reset_pwd);
        initData();
    }

    private void initData() {
        binding.loginPwdLayout.setPasswordVisibilityToggleDrawable(R.drawable.hzkl_pwd_selector);
        binding.registerBack.setOnClickListener(v -> finish());
        binding.resetBtn.setOnClickListener(view -> setPwd());
        new EditTextChangeBoolean().buttonEnable(binding.oldPwd, enable -> {
            oldEnable = enable;
            binding.resetBtn.setEnabled(oldEnable && newEnable);
        });
        new EditTextChangeBoolean().buttonEnable(binding.newPwd, enable -> {
            newEnable = enable;
            binding.resetBtn.setEnabled(oldEnable && newEnable);
        });
    }

    private void setPwd() {
        String oldPwd = binding.oldPwd.getText().toString();
        String newPwd = binding.newPwd.getText().toString();
        if (TextUtils.isEmpty(oldPwd)) {
            Toast.makeText(this, "请输入旧密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(newPwd)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!newPwd.matches(regex)) {
            Toast.makeText(this, "密码格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.setPwd(oldPwd, newPwd).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    //Toast.makeText(HZKLUserResetPwdActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        });
    }
}
